<!doctype html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>账单详情 - 智能水表抄表系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="/static/styles/main.css">
</head>

<body>
    <header class="header">
        <h1><i class="fas fa-tint"></i> 智能水表抄表系统</h1>
    </header>

    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm">
        <div class="container">
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
                aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/"><i class="fas fa-home"></i> 首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/history"><i class="fas fa-history"></i> 历史记录</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/bills"><i class="fas fa-file-invoice-dollar"></i> 账单管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/anomalies"><i class="fas fa-exclamation-triangle"></i> 异常记录</a>
                    </li>
                    {% if user and user.role == "admin" %}
                    <li class="nav-item">
                        <a class="nav-link" href="/water_plans"><i class="fas fa-project-diagram"></i> 水务规划</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/admin"><i class="fas fa-user-shield"></i> 管理控制台</a>
                    </li>
                    {% endif %}
                </ul>
                <div class="navbar-nav">
                    <span class="nav-item nav-link">
                        <i class="fas fa-user"></i> {{ user.username }}
                    </span>
                    <a class="nav-link" href="/logout"><i class="fas fa-sign-out-alt"></i> 退出登录</a>
                </div>
            </div>
        </div>
    </nav>

    <div class="container py-4">
        <!-- 消息提示 -->
        {% if success %}
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ success }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endif %}
        {% if error %}
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            {{ error }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endif %}

        <div class="row mb-4">
            <div class="col-md-12">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="/bills">账单管理</a></li>
                        <li class="breadcrumb-item active" aria-current="page">账单详情</li>
                    </ol>
                </nav>
                <div class="d-flex justify-content-between align-items-center">
                    <h2><i class="fas fa-file-invoice"></i> 账单详情</h2>
                    <div>
                        <a href="/bills" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> 返回列表
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-8">
                <div class="card shadow-sm mb-4">
                    <div class="card-header bg-light">
                        <h3 class="card-title mb-0">
                            <i class="fas fa-info-circle"></i> 账单信息
                            {% if bill.status == 'paid' %}
                            <span class="badge bg-success float-end">已支付</span>
                            {% elif bill.status == 'unpaid' %}
                            <span class="badge bg-warning float-end">待支付</span>
                            {% elif bill.status == 'pending_review' %}
                            <span class="badge bg-primary float-end">待审核</span>
                            {% elif bill.status == 'rejected' %}
                            <span class="badge bg-danger float-end">已拒绝</span>
                            {% endif %}
                        </h3>
                    </div>
                    <div class="card-body">
                        <table class="custom-table">
                            <tbody>
                                <tr>
                                    <th width="30%">账单编号</th>
                                    <td>{{ bill.id }}</td>
                                </tr>
                                <tr>
                                    <th>水表编码</th>
                                    <td>{{ bill.meter_code }}</td>
                                </tr>
                                <tr>
                                    <th>起始读数</th>
                                    <td>{{ bill.start_reading }} m³</td>
                                </tr>
                                <tr>
                                    <th>结束读数</th>
                                    <td>{{ bill.end_reading }} m³</td>
                                </tr>
                                <tr>
                                    <th>用水量</th>
                                    <td>{{ "%.2f"|format(bill.water_usage|float) }} m³</td>
                                </tr>
                                <tr>
                                    <th>账单金额</th>
                                    <td class="text-danger fw-bold">¥ {{ "%.2f"|format(bill.amount|float) }}</td>
                                </tr>
                                <tr>
                                    <th>账单日期</th>
                                    <td>{{ bill.bill_date }}</td>
                                </tr>
                                <tr>
                                    <th>状态</th>
                                    <td>
                                        {% if bill.status == 'paid' %}
                                        <span class="badge bg-success">已支付</span>
                                        {% elif bill.status == 'unpaid' %}
                                        <span class="badge bg-warning">待支付</span>
                                        {% elif bill.status == 'pending_review' %}
                                        <span class="badge bg-primary">待审核</span>
                                        {% elif bill.status == 'rejected' %}
                                        <span class="badge bg-danger">已拒绝</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                {% if bill.status == 'paid' %}
                                <tr>
                                    <th>支付时间</th>
                                    <td>{{ bill.payment_date }}</td>
                                </tr>
                                <tr>
                                    <th>支付方式</th>
                                    <td>{{ bill.payment_method }}</td>
                                </tr>
                                {% endif %}
                                {% if bill.reviewer_id and bill.review_date %}
                                <tr>
                                    <th>审核人ID</th>
                                    <td>{{ bill.reviewer_id }}</td>
                                </tr>
                                <tr>
                                    <th>审核日期</th>
                                    <td>{{ bill.review_date }}</td>
                                </tr>
                                {% endif %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="col-md-4">
                <div class="card shadow-sm mb-4">
                    <div class="card-header bg-light">
                        <h3 class="card-title mb-0"><i class="fas fa-calculator"></i> 费用与操作</h3>
                    </div>
                    <div class="card-body">
                        <div class="alert alert-info">
                            <i class="fas fa-info-circle"></i> 阶梯水价计算明细
                        </div>
                        <ul class="list-group mb-3">
                            {% set usage = bill.water_usage|float %}
                            {% if usage <= 10 %}
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                <div>
                                    <h6 class="my-0">基本水价 (0-10m³)</h6>
                                    <small class="text-muted">单价: ¥3.00/m³</small>
                                </div>
                                <span class="text-muted">{{ "%.2f"|format(usage) }} m³</span>
                                <span class="text-primary">¥ {{ "%.2f"|format(usage * 3) }}</span>
                            </li>
                            {% else %}
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                <div>
                                    <h6 class="my-0">基本水价 (0-10m³)</h6>
                                    <small class="text-muted">单价: ¥3.00/m³</small>
                                </div>
                                <span class="text-muted">10.00 m³</span>
                                <span class="text-primary">¥ 30.00</span>
                            </li>
                            {% endif %}

                            {% if usage > 10 and usage <= 30 %}
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                <div>
                                    <h6 class="my-0">阶梯水价 (10-30m³)</h6>
                                    <small class="text-muted">单价: ¥3.50/m³</small>
                                </div>
                                <span class="text-muted">{{ "%.2f"|format(usage - 10) }} m³</span>
                                <span class="text-primary">¥ {{ "%.2f"|format((usage - 10) * 3.5) }}</span>
                            </li>
                            {% elif usage > 30 %}
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                <div>
                                    <h6 class="my-0">阶梯水价 (10-30m³)</h6>
                                    <small class="text-muted">单价: ¥3.50/m³</small>
                                </div>
                                <span class="text-muted">20.00 m³</span>
                                <span class="text-primary">¥ 70.00</span>
                            </li>
                            {% endif %}

                            {% if usage > 30 %}
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                <div>
                                    <h6 class="my-0">阶梯水价 (30m³以上)</h6>
                                    <small class="text-muted">单价: ¥4.00/m³</small>
                                </div>
                                <span class="text-muted">{{ "%.2f"|format(usage - 30) }} m³</span>
                                <span class="text-primary">¥ {{ "%.2f"|format((usage - 30) * 4) }}</span>
                            </li>
                            {% endif %}
                        </ul>

                        <div class="d-flex justify-content-between mb-3">
                            <h5>总计金额:</h5>
                            <h5 class="text-danger">¥ {{ "%.2f"|format(bill.amount|float) }}</h5>
                        </div>

                        <!-- 操作按钮区 -->
                        {% if user.role == 'admin' and bill.status == 'pending_review' %}
                        <div class="mt-4">
                            <h5><i class="fas fa-user-shield"></i> 管理员审核操作</h5>
                            <form method="POST" action="/bill/{{ bill.id }}/review_action" class="d-inline">
                                <input type="hidden" name="action" value="approve">
                                <button type="submit" class="btn btn-success me-2">
                                    <i class="fas fa-check"></i> 批准
                                </button>
                            </form>
                            <form method="POST" action="/bill/{{ bill.id }}/review_action" class="d-inline">
                                <input type="hidden" name="action" value="reject">
                                <button type="submit" class="btn btn-danger">
                                    <i class="fas fa-times"></i> 拒绝
                                </button>
                            </form>
                        </div>
                        {% elif user.role == 'user' and bill.status == 'unpaid' %}
                        <div class="mt-4">
                             <a href="/pay/{{ bill.id }}" class="btn btn-primary btn-lg w-100">
                                <i class="fas fa-credit-card"></i> 立即支付
                            </a>
                        </div>
                        {% elif bill.status == 'paid' %}
                        <div class="alert alert-success mt-4">
                            <i class="fas fa-check-circle"></i> 此账单已于 {{ bill.payment_date }} 通过 {{bill.payment_method}} 支付完成。
                        </div>
                        {% elif bill.status == 'rejected' %}
                        <div class="alert alert-danger mt-4">
                            <i class="fas fa-times-circle"></i> 此账单已被管理员拒绝。
                        </div>
                        {% elif bill.status == 'pending_review' and user.role == 'user' %}
                         <div class="alert alert-info mt-4">
                            <i class="fas fa-hourglass-half"></i> 此账单正在等待管理员审核。
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <footer class="footer">
        <div class="container">
            <p>© 2024 智能水表抄表系统 | 提供水表读数和编码识别</p>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
        crossorigin="anonymous"></script>
</body>

</html>